package com.hddf.project.auth.service;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Map;

import com.hddf.project.auth.dao.KeyPairDao_MongoDB;
import com.hddf.project.auth.dao_impl.KeyPairDaoImpl_MongoDB;
import org.kommii.encryption.RSAHandler;
import com.kommii.mongodb_utils.Mongodb_ConnPools;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
public class KeyPairService {
	
	private static KeyPairService keypair_service = null;
	private static KeyPairDao_MongoDB keyPairDao_MongoDB = null;
	static {
		if(keypair_service==null){
			keypair_service = new KeyPairService();
		}
		if(keyPairDao_MongoDB==null){
			keyPairDao_MongoDB = new KeyPairDaoImpl_MongoDB();
		}
	}
	
	public static KeyPairService getInstance(){
		return keypair_service;
	}
	
	/**
	 * 向 mongodb 数据库添加密钥对 并返回mongodb中的 _id 
	 */
	public String addKeyPair(KeyPair kp){
		DBCollection coll  = null;
		try{
			String pubKeyString = getPubKeyString(kp);
			Map map = getPriKeyMap(kp);
			coll = Mongodb_ConnPools.getMongoDB_Coll("key_pair");
			return keyPairDao_MongoDB.add(pubKeyString,map,coll);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public PrivateKey getPrivateKeyById(String id){
		DBCollection coll  = null;
		Map map = null;
		PrivateKey prik = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll("key_pair");
			DBObject obj = keyPairDao_MongoDB.getPriKeyById(id, coll);
			if(null!=obj){
				map = (Map)obj.get("prik_map");
				prik = getPriKeyFromMap(map);
			}
			return prik;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public Map getPrivateKeyMapById(String id){
		DBCollection coll  = null;
		Map map = null;	
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll("key_pair");
			DBObject obj = keyPairDao_MongoDB.getPriKeyById(id, coll);
			if(null!=obj){
				map = (Map)obj.get("prik_map");
			}
			return map;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public String getPublicKeyStringById(String id){
		DBCollection coll  = null;
		String pubk_str = null;
		
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll("key_pair");
			DBObject obj = keyPairDao_MongoDB.getPubKeyById(id, coll);
			if(null!=obj){
				pubk_str = (String)obj.get("pubk");
			}
			return pubk_str;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public PublicKey getPublicKeyById(String id){
		DBCollection coll  = null;
		String pubk_str = null;
		PublicKey pubk = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll("key_pair");
			DBObject obj = keyPairDao_MongoDB.getPubKeyById(id, coll);
			if(null!=obj){
				pubk_str = (String)obj.get("pubk");
				pubk = getPubKeyFromString(pubk_str);
			}
			return pubk;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean delete(String id){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll("key_pair");
			return keyPairDao_MongoDB.delete(id, coll);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	
	/**
	 * 获取RSA密钥对
	 * @return
	 */
	public KeyPair getKeyPair(){
		KeyPair kp = RSAHandler.generateRSAKeyPair(512);
		return kp;
	}
	
	/**
	 * 获取公钥字符串
	 * @param kp
	 * @return
	 */
	public String getPubKeyString(KeyPair kp){
		PublicKey pubk = kp.getPublic();
		return RSAHandler.encodePublicKeyToString(pubk);
	}
	
	/**
	 * 通过公钥字符串生成公钥
	 * @param kp
	 * @return
	 */
	public PublicKey getPubKeyFromString(String str){
		PublicKey pubk = RSAHandler.decodePublicKeyFromString(str);
		return pubk;
	}
	
	/**
	 * 获取私钥map
	 * @param kp
	 * @return
	 */
	public Map getPriKeyMap(KeyPair kp){
		PrivateKey prik = kp.getPrivate();
		return RSAHandler.encodePrivateKeyToMap(prik);
	}
	
	/**
	 * 通过map里的参数，生成私钥
	 * @param map
	 * @return
	 */
	public PrivateKey getPriKeyFromMap(Map map){
		PrivateKey prik = RSAHandler.decodePrivateKeyFromMap(map);
		return prik;
	}
	
	

}
